var prefix = jsctx + "/pileFault";

var pileFault = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    /**
     * 导出列表
     */
    function exportList() {
        $.fileDownload(prefix + "/export", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                pileFaultType: $("#pileFaultType").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPileFault",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    rightFixedColumns: false,
                    showColumns: false,
                    // cache: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            pileFaultType: $("#pileFaultType").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'pileFaultCode', title: '编码', align: 'left', width: '80'},
                        {field: 'pileFaultType', title: '故障类型', width: '80', align: 'center'},
                        {field: 'pileFaultLevel', title: '故障等级', width: '80', align: 'center'},
                        {field: 'pileFaultDetail', title: '故障描述', width: '80', align: 'center'},
                        {field: 'createdTime', title: '创建时间', width: '200', align: 'center'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '300', align: 'center',
                            class: 'operate-td',
                            formatter: function (value, row, index) {
                                var action = '';
                                action += '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="pileFault.editPileFault(\''
                                    + row.pileFaultId
                                    + '\')">编辑</a> ';
                                action += '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="删除" onclick="pileFault.deletePileFault(\''
                                    + row.pileFaultId
                                    + '\')">删除</a> ';
                                return action;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addPileFault() {
        location.href = prefix + "/addPileFault";
    }
    function editPileFault(pileFaultId) {
        location.href = prefix + "/editPileFault/"+pileFaultId;
    }
    function deletePileFault(pileFaultId) {
        layer.confirm('确定要删除选中的故障类型？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deletePileFault",
                type: "post",
                data: {
                    'pileFaultId': pileFaultId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        location.href = prefix + "/index";
    }


    /*function exportPileFault() {
        $.fileDownload(prefix + "/exportPileFault", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                stationName: $("#labelName").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }*/

    /*function updateForTop(elecStationId, topRemark) {
        if (topRemark != '' && topRemark != 'null') {
            $("#showPlaceTag").val(showPlaceTag)
        }

        $('#exampleModal').modal('show')

        $("#submitForm").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("showPlaceTag", $("#showPlaceTag").val());
            formData.append("elecStationId", elecStationId);
            $.ajax({
                url: prefix + "/updateForTop",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                    }

                }
            })
        })
    }*/

  /*  function updateForNoTop(elecStationId) {
        layer.confirm('确定要取消置顶？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateForNoTop",
                type: "post",
                data: {
                    'elecStationId': elecStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("取消成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }*/


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addPileFault: addPileFault,
        editPileFault: editPileFault,
        exportList: exportList,
        deletePileFault: deletePileFault
    };
})();

var addPileFault = (function () {
    function init() {
        validateRule();
    };
    var isajaxsend = true;

    function save() {
        var formData = new FormData($("#frmPileFault")[0]);
        if (isajaxsend) {
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdatePileFault",
                data: formData,
                processData: false,
                contentType: false,
                // async: false,
                error: function (request) {
                    isajaxsend = true
                    console.log(request)
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = false
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message)
                    }

                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmPileFault").validate({
            rules: {
                pileFaultCode: {
                    required: true
                },pileFaultType: {
                    required: true
                },
                pileFaultLevel: {
                    required: true
                }
            },
            messages: {
                pileFaultCode: {
                    required: icon + "请输入故障代码",
                },pileFaultType: {
                    required: icon + "请输入故障类型",
                },
                pileFaultLevel: {
                    required: icon + "请输入故障级别",
                },
            },
            submitHandler: function (form) {
                save();
            }
        })
    }
    return {
        init: init
    };
})();

/*

var elecStationOut = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        /!*if (isajaxsend) {
            var formData = new FormData($("#frmElecStationOut")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateElecStationOut",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        javascript:history.back(-1);
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }*!/
    }

    function validateRule() {
       /!* var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmElecStationOut").validate({
            rules: {
                outType: {
                    required: true
                },
                printerType: {
                    required: true
                },
                isFreeParking: {
                    required: true
                },
                isNeedNumber: {
                    required: true
                }
            },
            messages: {
                outType: {
                    required: icon + "请选择出场方式"
                },
                printerType: {
                    required: icon + "请选择打印机类型"
                },
                isFreeParking: {
                    required: icon + "请选择是否免费2个小时停车"
                },
                isNeedNumber: {
                    required: icon + "请选择是否必须车牌号"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })*!/
    }

    return {
        init: init
    };
})();
*/
